package cn.xuan.zp.web.authenticate;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xuan.zp.service.log.Annotation.LogAnnotation;


/***
 * 用户信息认证
 * @author XuanZP
 *
 */
@Controller
@RequestMapping("/user")
public class Authen {
    private static final Logger LOGGER = LoggerFactory.getLogger(Authen.class);
    
    @Autowired
	@Qualifier("passwordService")
	private PasswordService passwordService;
	
	@RequestMapping("/loginn/{loginName}/{pwd}")
	public Object loginn(@PathVariable("loginName") String loginName,@PathVariable("pwd")String pwd) {
//		pwd = passwordService.encryptPassword(pwd);
//		LOGGER.info("pwd==>" + pwd);
		UsernamePasswordToken token = new UsernamePasswordToken(loginName, pwd);
		token.setRememberMe(false);
		SecurityUtils.getSubject().login(token);
		return "test";		
	}


	@LogAnnotation(targetType = "login",action = "用户登录",remark = "12312")
	/*@RequiresPermissions("sys:perms:login")*/
	@ResponseBody
	@RequestMapping("/encryptText/{text}")
	public Map<String, Object> encrypt(@PathVariable String text) {
		LOGGER.info("text===>" + text);

		text = getPwdEncrypt(text);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", text);

		return map;
	}
	private String getPwdEncrypt(String password) {
		String encryptText = passwordService.encryptPassword(password);
		LOGGER.info("result=" + encryptText);
		return encryptText;
	}
}
